package com.lanswon.carlocationsearch.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.hutool.json.JSONObject;
import lombok.experimental.UtilityClass;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @Description
 * @Author zsw
 * @Date 2019/12/5 12:20
 * @Version V1.0
 **/
@UtilityClass
public class ExportExcelUtil {

    public Workbook createWorkbook(List<JSONObject> list, int num) throws Exception {


        List<ExcelExportEntity> colList = new ArrayList<>();


        ExcelExportEntity importGroup = new ExcelExportEntity("导入数据", "importData");

        List<ExcelExportEntity> importColList = new ArrayList<>();
        importColList.add(new ExcelExportEntity("车牌", "carNum"));
        importColList.add(new ExcelExportEntity("过车时间", "dateTime"));
        importColList.add(new ExcelExportEntity("违法地点", "illegalPlace"));

        importGroup.setList(importColList);
        colList.add(importGroup);

        ExcelExportEntity resultGroup = new ExcelExportEntity("返回结果", "result");

        List<ExcelExportEntity> resultColList = new ArrayList<>();

        for(int i=0;i<num;i++){
            resultColList.add(new ExcelExportEntity("过车地点", "currentPassCarPlace"+i));
            resultColList.add(new ExcelExportEntity("过车时间", "currentPassCarDateTime"+i));
        }


        resultGroup.setList(resultColList);
        colList.add(resultGroup);



        //拉数据
        List<Map<String, Object>> dataList = new ArrayList<>();

        for(JSONObject carDto :list){
            Map<String, Object> values = new HashMap<>();


            List<Map<String, Object>> importData = new ArrayList<>();
            Map<String, Object> importDataMap = new HashMap<>();

            importDataMap.put("carNum",carDto.get("carNum"));
            importDataMap.put("dateTime",carDto.get("dateTime"));
            importDataMap.put("illegalPlace",carDto.get("illegalPlace"));

            importData.add(importDataMap);


            values.put("importData",importData);

            List<Map<String, Object>> result = new ArrayList<>();

            Map<String, Object> exportDataMap = new HashMap<>();



//            List<CPicrecordDto> list1 = carDto.getList();
            for(int i=0;i<num;i++){
                exportDataMap.put("currentPassCarPlace"+i,carDto.get("passCarPlace"+i));
                exportDataMap.put("currentPassCarDateTime"+i,carDto.get("passCarDate"+i));
            }

            result.add(exportDataMap);
            values.put("result",result);

            dataList.add(values);
        }
        ExportParams exportParams = new ExportParams("导出数据", "过车记录");
        exportParams.setType(ExcelType.XSSF);

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, colList,
                dataList);
        for(int i=0;i<3+2*num;i++){
            workbook.getSheetAt(0).autoSizeColumn(i,true);
        }


//        FileOutputStream fos = new FileOutputStream("D:/价格分析表.xlsx");
//        workbook.write(fos);
//        fos.close();

        return workbook;
    }
/*
    public static void main(String[] args) throws Exception {


    }


    public void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,boolean isCreateHeader, HttpServletResponse response){
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);



        defaultExport(list, pojoClass, fileName, response, exportParams);

    }
    public void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    }


    private  void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
        if (workbook != null){
            downLoadExcel(fileName, response, workbook);
        }

    }
*/
    public  void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {

        }
    }
}